# frozen_string_literal: true

class SchemaVariable < ApplicationRecord
  include Ordinalable

  module IoType
    extend ConstantEnum

    INPUT = 'input'
    OUTPUT = 'output'
  end

  has_many :decision_table_headers

  belongs_to :schema
  belongs_to :variable_type

  validates :name,
            presence: true,
            format: { with: /\A[a-zA-z_][a-zA-z0-9_]*\z/ },
            uniqueness: { scope: %i[schema_id io_type], case_sensitive: true }
  validates :io_type, presence: true, inclusion: { in: IoType.values }
  validates :ordinal, presence: true, uniqueness: { scope: %i[schema_id io_type], case_sensitive: true }

  # params = {
  #   ofo_id: 1,
  #   ofo_name: "Kira Zhou"
  # }
  def validate?(params)
    value = params[name] || params[name.to_sym]

    return !required? if value.blank?

    variable_type.validate?(value)
  end
end
